package com.longcai.piaojia.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.UpdateAdvataGet;
import com.longcai.piaojia.dialog.PictureDialog;
import com.longcai.piaojia.fragment.MineFragment;
import com.longcai.piaojia.util.UtilPermission;
import com.longcai.piaojia.util.VirtualBackground;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.entity.AppCrop;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilBase64;
import com.zcx.helper.util.UtilFile;
import com.zcx.helper.util.UtilSDCard;
import com.zcx.helper.util.UtilToast;

import java.io.File;

/**
 * Created by Administrator on 2016/8/2.
 */
public class MyAccountManagerActivity extends BasePictureActivity implements View.OnClickListener {


    @BoundView(R.id.account_manager_headimg_rl) private RelativeLayout account_manager_headimg_rl;
    @BoundView(R.id.account_managet_nickname_rl) private RelativeLayout account_managet_nickname_rl;
    @BoundView(R.id.account_managet_name_rl) private RelativeLayout account_managet_name_rl;
    @BoundView(R.id.account_manager_recive_address_rl) private RelativeLayout account_manager_recive_address_rl;
    @BoundView(R.id.account_manager_edit_phonenum_rl) private RelativeLayout account_manager_edit_phonenum_rl;
    @BoundView(R.id.account_manager_edit_password_rl) private RelativeLayout account_manager_edit_password_rl;
    @BoundView(R.id.account_manager_headimg_iv) private ImageView account_manager_headimg_iv;
    @BoundView(R.id.account_managet_nickname_tv) private TextView account_managet_nickname_tv;
    @BoundView(R.id.account_managet_name_tv) private TextView account_managet_name_tv;

    private String path, nickname, truename;

    private UpdateAdvataGet updateAdvataGet = new UpdateAdvataGet(new AsyCallBack<String>(){

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {

            GlideLoader.getInstance().get(MyAccountManagerActivity.this, path, account_manager_headimg_iv, R.mipmap.mine_headimg, GlideLoader.TYPE_IMAGE_CIRCLE);

            MineFragment.getUserAdvert getUserAdvert = (MineFragment.getUserAdvert) getAppCallBack(MineFragment.class);

            getUserAdvert.updateAdvert(path);

//            UtilFile.deletePathFile(path);

            UtilToast.show(MyAccountManagerActivity.this, toast);
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(MyAccountManagerActivity.this, toast);

//            UtilFile.deletePathFile(path);

        }

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_account_manager);

        setBackTrue();

        setTitleName(getResources().getString(R.string.account_manager_title_text));

        Bundle bundle = getIntent().getExtras();

        nickname = bundle.getString("nickname","");
        truename = bundle.getString("truename","");

//        GlideLoader.getInstance().init(R.id.default_id, R.mipmap.mine_headimg);
        GlideLoader.getInstance().get(MyAccountManagerActivity.this, bundle.getString("avatar", ""), account_manager_headimg_iv, R.mipmap.mine_headimg, GlideLoader.TYPE_IMAGE_CIRCLE);
        account_managet_nickname_tv.setText(nickname);
        account_managet_name_tv.setText(truename);

//        String[] permission = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
//
//        UtilPermission.Permission(MyAccountManagerActivity.this, permission);

        account_manager_recive_address_rl.setOnClickListener(this);
        account_managet_name_rl.setOnClickListener(this);
        account_managet_nickname_rl.setOnClickListener(this);
        account_manager_headimg_rl.setOnClickListener(this);
        account_manager_edit_phonenum_rl.setOnClickListener(this);
        account_manager_edit_password_rl.setOnClickListener(this);


        setAppCallBack(new getUserInfo());

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            //我的收货地址
            case R.id.account_manager_recive_address_rl:

                startActivity(new Intent(this, MyReceiveAddressActivity.class).putExtra("isChoose", false));

                break;
            //姓名
            case R.id.account_managet_name_rl:

//                startVerifyActivity(EditNameActivity.class);
                startActivity(new Intent(MyAccountManagerActivity.this, EditNameActivity.class).putExtra("truename", truename));

                break;
            //昵称
            case R.id.account_managet_nickname_rl:

//                startVerifyActivity(EditNickNameActivity.class);
                startActivity(new Intent(MyAccountManagerActivity.this, EditNickNameActivity.class).putExtra("nickname", nickname));

                break;
            //修改密码
            case R.id.account_manager_edit_password_rl:

                startVerifyActivity(EditPasswordActivity.class);

                break;

            //修改电话号码
            case R.id.account_manager_edit_phonenum_rl:

                startVerifyActivity(ConfirmOldPhoneActivity.class);

                break;

            //头像
            case R.id.account_manager_headimg_rl:

                new PictureDialog(context) {
                    @Override
                    protected void onCamera() {

                        startCamera(null);
                    }

                    @Override
                    protected void onAlbum() {

                        startAlbum(null);
                    }
                }.show();

                break;
        }
    }


    @Override
    protected AppCrop getCrop() {
        return new AppCrop().setCrop(true);
    }

    @Override
    protected void resultPhotoPath(ImageView imageView, String s) {

        path = s;

        updateAdvataGet.user_id = BaseApplication.BasePreferences.readUserId();
        updateAdvataGet.image = new File(s);
        updateAdvataGet.execute(this);

    }

    public class getUserInfo implements AppCallBack {

        public void updateNickName(String s){

            account_managet_nickname_tv.setText(s);
            nickname = s;

        }

        public void updateName(String s){

            account_managet_name_tv.setText(s);
            truename = s;

        }

    }

}
